package final_demo;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class FinalFieldExample {
    final int x;
    int y; 
    static FinalFieldExample f;

    public FinalFieldExample() {
        x = 3;
        y = 4;
    }

    static void writer() {
        f = new FinalFieldExample();
    } 

    static void reader() {
        if (f != null) {
            int j = f.y;  // could see 0
            int i = f.x;  // guaranteed to see 3
            assert i == 3 : "x != 3";
            assert j == 4 : "y != 4";
        }
    }

    public static void main(String[] args) {
        final int n = 10000; // 测试次数

        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < n; i++) {
            executorService.execute(FinalFieldExample::writer);
            executorService.execute(FinalFieldExample::reader);
        }
        System.out.println("ok");
        executorService.shutdown();
    }
}